
var scale;

$(document).ready(function(){
	setScale(1);
	initSettings();
    init();
});

function setScale(s) {
	scale = s;
}
function initSettings(){
	
    ctx = document.getElementById('canvas').getContext('2d'); 

    screenWidth = parseInt($("#canvas").attr("width"));
    screenHeight = parseInt($("#canvas").attr("height"));
    
    pitchW = 400 * scale;
    pitchH = 200 * scale;
    pitchX = (screenWidth - pitchW)/2;
    pitchY = (screenHeight - pitchH)/2;
    
    seatS = 2 * scale;
    
    pitchImg = new Image();
    pitchImg.src = "resources/images/pitch.png";
 
}

function init(){
   
	var capacity = document.forms['stadiumForm'].elements['stadiumForm:capacity'].value;
	var s = new Array();
	s[0]= document.forms['stadiumForm'].elements['stadiumForm:tribune0'].value;
	s[1]= document.forms['stadiumForm'].elements['stadiumForm:tribune1'].value;
	s[2]= document.forms['stadiumForm'].elements['stadiumForm:tribune2'].value;
	s[3]= document.forms['stadiumForm'].elements['stadiumForm:tribune3'].value;
	
	init2(capacity, s[0], s[1], s[2], s[3]);
	
}  
function init2(capacity, s0, s1, s2, s3) {
	
	initSettings();
	s = new Array();
	s[0]= s0;
	s[1]= s1;
	s[2]= s2;
	s[3]= s3;
	
	//----walidacja-----
	if(capacity < 5000) capacity = 5000;
	else if (capacity > 50000) capacity = 50000;
	
	for(var i = 0 ; i < 4 ; i++){
		if(s[i] < 1) s[i] = 1;
		else if(s[i] > 5) s[i] = 5;
	}
	//------------------
	
	tribuneCapacity = capacity /4;
	
	//tribune 0 & 1
	m1 = tribuneCapacity;
	x1 = pitchW;
	row1 = m1 / (x1 / seatS) + 1;
	y1 = seatS * row1;
	//tribune 2 & 3
	m2 = tribuneCapacity;
	y2 = pitchH + 2 * y1;
	row2 = m2 / (y2 / seatS) + 1; 
	x2 = seatS * row2;
	
	t = new Array();
	t[0]= new PObject(pitchX, pitchY - y1 , x1, y1);
	t[1]= new PObject(pitchX, pitchY + pitchH , x1, y1);
	t[2]= new PObject(pitchX -x2, pitchY - (y2 - pitchH)/2 , x2, y2);
	t[3]= new PObject(pitchX + pitchW, pitchY - (y2 - pitchH)/2 , x2, y2);
	
	
	sectors = new Array();
	for(var i = 0 ; i < t.length ; i++)
		sectors[i] = new Array();
	
	for(var i = 0 ; i < t.length ; i++){
		for(var j = 0 ; j < s[i] ; j++) {
			//north & south
			if(i < 2){
				width = t[i].w /s[i];
				sectors[i][j] = new PObject(t[i].x + j * width, t[i].y, width, t[i].h);		
			}
			//west && east
			else {
				height = t[i].h /s[i];
				sectors[i][j] = new PObject(t[i].x, t[i].y + j * height, t[i].w, height);	
			}
		}
	}
	
	render();
}
function render() {
	ctx.clearRect(0, 0, screenWidth, screenHeight);    	    
	
	//pitch
	ctx.fillStyle = "rgb(0,200,0)";
	ctx.fillRect (pitchX, pitchY, pitchW, pitchH);
	//ctx.drawImage(pitchImg, pitchX, pitchY);
	
	ctx.fillStyle = "rgb(0,0,0)";
	//tribunes
	/*
	for(var i = 0 ; i < t.length ; i++)
		drawRectangle(t[i].x, t[i].y, t[i].w, t[i].h);
	*/
	
	asci = 65;
	for(var i = 0 ; i < 4 ; i++)
		for(var j = 0 ; j < sectors[i].length ; j++){
			drawRectangle(sectors[i][j].x, sectors[i][j].y, sectors[i][j].w, sectors[i][j].h);
			ctx.fillText(String.fromCharCode(asci++),
					sectors[i][j].x + sectors[i][j].w/2,
					sectors[i][j].y + sectors[i][j].h/2  );
		}

	 
}

function PObject(x,y,w,h) {
	
	this.x = x;
	this.y = y;
	this.w = w;
	this.h = h;
		
}
function drawRectangle( x, y, w, h) {			
	  ctx.beginPath();
	  ctx.rect(x, y, w, h);
	  ctx.closePath();
	  ctx.stroke();
	}


